package com.happy.seatwork;

import java.util.Scanner;

public class bubble_sort {
    //冒泡排序方法
    public static void sort(int ... num){
        int[] arr = num;
        for (int i  = 0; i < arr.length - 1; i++){
            for (int j = 0; j < arr.length - 1- i; j++){
                //相邻两个数进行比较
                if (arr[j] > arr[j + 1]){
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        //将排好序的数组输出
        System.out.println("从小到大排列为：");
        for (int t = 0; t < arr.length;t++) {
            System.out.println(arr[t] + " ");
        }
    }

    public static void main(String args[]){
        int[] arr = new int[8];
        //从键盘输入数组
        System.out.println("请随机输入8个整数 ");
        for (int i = 0; i < arr.length; i++){
            Scanner scanner = new Scanner(System.in);
            arr[i] = (scanner.nextInt());
        }
        //调用方法，进行冒泡排序并输出
        sort(arr);
    }
}
